import io
import sys
import logging
import os

from config import configUtil


def mkdir(path):
    path = path.strip()
    is_exists = os.path.exists(path)
    if not is_exists:
        os.makedirs(path)
        print(path + ' success')
        return True


mkdir(configUtil.getLogFilePath())
formatter = logging.Formatter('%(asctime)s %(levelname)-8s: (%(name)s)%(pathname)s %(message)s')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')

logger = logging.getLogger("LCFCKPI")

io_wrapper = io.TextIOWrapper(sys.stdout.detach(), encoding='utf-8', line_buffering=True)
sys.stdout = sys.__stdout__ = io_wrapper
text_io_wrapper = io.TextIOWrapper(sys.stderr.detach(), encoding='utf-8', line_buffering=True)
sys.stderr = sys.__stderr__ = text_io_wrapper

console_handler = logging.StreamHandler(io_wrapper)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)

fh = logging.FileHandler(filename=configUtil.getlogFileName(), encoding='utf-8')
fh.setFormatter(formatter)
logger.addHandler(fh)
logger.setLevel(logging.DEBUG)
